﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.main {
	import air.net.URLMonitor;
	import air.update.ApplicationUpdaterUI;
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.display.NativeWindowDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.StatusEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import nl.sandmen.team.events.PlaysManagerEvent;
	import nl.sandmen.team.events.TrayMenuEvent;
	import nl.sandmen.team.fonts.Fonts;
	import nl.sandmen.team.gui.AboutWindow;
	import nl.sandmen.team.tray.TrayIcon;
	import nl.sandmen.team.tray.TrayMenu;
	/**
	 * @author Team Sandmen
	 */
	public class Main extends Sprite {
		/**
		 * Prevents the user from performing two show/hide actions when "double clicking" the tray icon.
		 */
		private var antiDoubleClickTimer:Timer;
		private var applicationUpdater:ApplicationUpdaterUI;
		/**
		 * Bit 0: Loading/loaded.
		 * Bit 1: GUI loaded/PlaysManager added to stage.
		 * Bit 2: About window opened.
		 */
		private var bits:uint = 0;
		private var playsManager:PlaysManager;
		private var urlMonitor:URLMonitor;
		public function Main():void {
			NativeApplication.nativeApplication.icon.bitmaps = TrayIcon.trayIcons;
			NativeApplication.nativeApplication.icon.addEventListener(MouseEvent.CLICK, iconClicked);
			if (NativeApplication.supportsSystemTrayIcon) {
				(NativeApplication.nativeApplication.icon as SystemTrayIcon).menu = TrayMenu.trayMenu;
			}
			stage.scaleMode = StageScaleMode.NO_SCALE;
			TitleController.initialize(stage.nativeWindow);
			TitleController.setTitle("Connecting to 8bitpeoples.com");
			Fonts.initialize();
			urlMonitor = new URLMonitor(new URLRequest("http://8bitpeoples.com"));
			urlMonitor.addEventListener(StatusEvent.STATUS, networkStatus);
			urlMonitor.start();
			playsManager = new PlaysManager();
			TrayMenu.trayEventDispatcher.addEventListener(TrayMenuEvent.ABOUT, about);
			TrayMenu.trayEventDispatcher.addEventListener(TrayMenuEvent.EXIT, exit);
			TrayMenu.trayEventDispatcher.addEventListener(TrayMenuEvent.SHOW_HIDE, toggleVisible);
			playsManager.addEventListener(PlaysManagerEvent.SHOW_HIDE, toggleVisible);
			playsManager.addEventListener(PlaysManagerEvent.EXIT, exit);
			antiDoubleClickTimer = new Timer(500, 1);
			applicationUpdater = new ApplicationUpdaterUI();
			applicationUpdater.updateURL = "http://air-8bitplays.googlecode.com/svn/trunk/data/update.xml";
			applicationUpdater.delay = 2;
			applicationUpdater.isCheckForUpdateVisible = false;
			applicationUpdater.initialize();
		}
		private function about(e:TrayMenuEvent):void {
			if (!(bits & 4)) {
				var aboutWindow:AboutWindow = new AboutWindow(applicationUpdater.currentVersion);
				aboutWindow.addEventListener(Event.CLOSE, aboutClose);
				aboutWindow.activate();
				bits ^= 4;
			}
		}
		private function aboutClose(e:Event):void {
			e.currentTarget.removeEventListener(Event.CLOSE, aboutClose);
			bits ^= 4;
		}
		private function exit(e:Event = null):void {
			TitleController.stop();
			NativeApplication.nativeApplication.exit();
		}
		private function iconClicked(e:MouseEvent):void {
			if (!antiDoubleClickTimer.running) {
				antiDoubleClickTimer.start();
				toggleVisible();
			}
		}
		private function get maskRect():Sprite {
			var mask:Sprite = new Sprite();
			mask.graphics.beginFill(0);
			mask.graphics.drawRoundRect(0, 0, 800, 600, 8);
			mask.graphics.endFill();
			return mask;
		}
		private function set maskRect(value:Sprite):void {
			throw new Error("maskRect property should not be set.");
		}
		private function networkStatus(e:StatusEvent):void {
			// The urlMonitor property monitors the connection between the user and 8bitpeoples.com, if the connection is available
			//  the playsManager property receives a "green light" which means it can start downloading contents from the website.
			if (urlMonitor.available && !(bits & 1)) {
				playsManager.greenLight();
				bits ^= 1;
			}
		}
		private function toggleVisible(e:Event = null):void {
			if (stage.nativeWindow.visible) {
				try {
					stage.nativeWindow.visible = false;
				} catch (e:Error) {
				}
			} else {
				try {
					if (!(bits & 2)) {
						addChild(playsManager).mask = maskRect;
						bits ^= 2;
					}
					stage.nativeWindow.visible = true;
					stage.nativeWindow.activate();
				} catch (e:Error) {
				}
			}
		}
	}
}